package com.health.activity.settings;

import com.health.NovaActivity;
import com.health.NovaApplication;
import com.health.R;
import com.health.UpdateVersion;
import com.health.model.SystemSettings;
import com.health.tools.DownLoadTools;
import com.health.tools.GetPostJTYS;
import com.health.tools.GlobalConts;
import com.health.tools.SPUtil;
import com.novabluetooth.services.ktxyj.Constants;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static com.health.tools.SPUtil.SP_NAME;
import static com.health.tools.SPUtil.STR_ORIGIN_CODE;

/**
 * 系统设置
 * 
 * @author 田相伟
 *
 */
public class SystemSettingActivity extends NovaActivity implements View.OnClickListener {
	private Button set_update;// 更新

	public EditText ftp_path;// ftp地址
	public EditText ftp_account;// ftp帐号
	public EditText ftp_passward;// ftp密码
	public EditText db_path;// 数据库地址
	public EditText et_zzjgCode;// 组织机构编号
	public EditText date_tongbu_path;// 数据同步地址
	public EditText shangchuan_yanzheng;// 上传验证接口
	public EditText shengji_hao;// 产品升级号
	public EditText zidong_shengji;// 自动升级
	public Button save;// 保存
	public Button cancle;// 取消
	private static String pathStr = Environment.getExternalStorageDirectory() + GlobalConts.TYPE_DBFILENAME;
	private Button fanhui;
	private Button up;
	private TextView title;
	private Button set_xzq;
	public EditText et_uploadaddress;// 公卫上传地址

	private Button set_dzjmjkk;// 设置居民电健康卡

	private EditText et_upload_jtys_address;//家庭医生上传地址

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_systemsetting);
		findViews();

		setListeners();
		initData();

	}

	public void initData() {
		if (TextUtils.isEmpty(db_path.getText())) {
			db_path.setText(pathStr + "/KioskDB.db");
		}
		SystemSettings sys = SPUtil.getSysSettings(SystemSettingActivity.this);
		if (sys != null) {

			if (!TextUtils.isEmpty(sys.getFtpAddress())) {
				ftp_path.setText(sys.getFtpAddress());
			}
			if (!TextUtils.isEmpty(sys.getFtpAccount())) {
				ftp_account.setText(sys.getFtpAccount());
			}
			if (!TextUtils.isEmpty(sys.getFtpPassword())) {
				ftp_passward.setText(sys.getFtpPassword());
			}
			if (!TextUtils.isEmpty(sys.getDb_path())) {
				db_path.setText(sys.getDb_path());
			}
			if (!TextUtils.isEmpty(sys.getDate_tongbu_path())) {
				date_tongbu_path.setText(sys.getDate_tongbu_path());
			}
			if (!TextUtils.isEmpty(sys.getShangchuan_yanzheng())) {
				shangchuan_yanzheng.setText(sys.getShangchuan_yanzheng());
			}
			if (!TextUtils.isEmpty(sys.getShengji_hao())) {
				shengji_hao.setText(sys.getShengji_hao());
			}
			if (!TextUtils.isEmpty(sys.getZidong_shengji())) {
				zidong_shengji.setText(sys.getZidong_shengji());
			}
		}
		et_uploadaddress.setText(TextUtils.isEmpty(SPUtil.getSTR_UPLOAD_ADDRESS(SystemSettingActivity.this)) ? ""
				: SPUtil.getSTR_UPLOAD_ADDRESS(SystemSettingActivity.this));
		et_upload_jtys_address.setText(TextUtils.isEmpty(SPUtil.getSTR_UPLOAD_JTYS_ADDRESS(SystemSettingActivity.this)) ? ""
				: SPUtil.getSTR_UPLOAD_JTYS_ADDRESS(SystemSettingActivity.this));
	}

	/**
	 * 显示同步数据
	 * 
	 * @param context
	 * @author 王婷婷
	 * @date 2017年3月14日 下午2:51:30
	 */
	private void showSynDialog(final Context context) {
		// 获得SharedPreferences对象
		// 获得SharedPreferences.Editor对象
		LayoutInflater inflater = LayoutInflater.from(this);
		final View textEntryView = inflater.inflate(R.layout.syn_dialog, null);
		final EditText ed_xzbh = (EditText) textEntryView.findViewById(R.id.ed_xzbh);
		ed_xzbh.setText(SPUtil.getstr_xzq(SystemSettingActivity.this));
		final EditText ed_pwd = (EditText) textEntryView.findViewById(R.id.ed_pwd);
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		builder.setView(textEntryView);
		builder.setPositiveButton("同步", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String str_xzbh = ed_xzbh.getText().toString();
				if (TextUtils.isEmpty(str_xzbh) || str_xzbh.length() != 6) {
					showTextDialog("请输入正确的行政编号");
					return;
				}
				String str_pwd = ed_pwd.getText().toString();
				if (str_pwd.equals("nova8888")) {
					String fileURl = GlobalConts.getUrlPost() + GetPostJTYS.URL_DBSYNC + "?orgDistrictId=" + str_xzbh;
					Log.w("eeeeeeeeeeeeee", fileURl);
					if (!TextUtils.isEmpty(fileURl)) {
						SPUtil.setstr_xzq(SystemSettingActivity.this, str_xzbh);
						DownLoadTools downLoad = new DownLoadTools(context);
						downLoad.downloadFile(fileURl, NovaApplication.dbPath);
					}
				} else {
					showTextDialog("密码有误，请重新输入");
				}

			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}

	@Override
	public void findViews() {
		ftp_path = (EditText) findViewById(R.id.ftp_path);
		ftp_account = (EditText) findViewById(R.id.ftp_acount);
		ftp_passward = (EditText) findViewById(R.id.ftp_passward);
		db_path = (EditText) findViewById(R.id.db_path);
		et_zzjgCode = (EditText) findViewById(R.id.et_zzjgCode);
		date_tongbu_path = (EditText) findViewById(R.id.date_tongbu_path);
		shangchuan_yanzheng = (EditText) findViewById(R.id.shangchuan_yanzheng);
		save = (Button) findViewById(R.id.save);
		cancle = (Button) findViewById(R.id.cancle);
		shengji_hao = (EditText) findViewById(R.id.shengji_hao);
		set_update = (Button) findViewById(R.id.set_update);
		set_dzjmjkk = (Button) findViewById(R.id.set_dzjmjkk);

		int vercode = GlobalConts.getVerCode(getApplicationContext());
		if (vercode == -1) {
			shengji_hao.setText("1");
		} else {
			shengji_hao.setText(String.valueOf(vercode));
		}

		zidong_shengji = (EditText) findViewById(R.id.zidong_shengji);
		fanhui = (Button) findViewById(R.id.btn_back);
		up = (Button) findViewById(R.id.btn_up);
		up.setVisibility(View.GONE);
		title = (TextView) findViewById(R.id.title);
		title.setText("系统设置");
		set_xzq = (Button) findViewById(R.id.set_xzq);
		et_uploadaddress = (EditText) findViewById(R.id.et_uploadaddress);
		et_upload_jtys_address = (EditText) findViewById(R.id.et_upload_jtys_address);
	}

	@Override
	public void setListeners() {
		set_dzjmjkk.setOnClickListener(this);
		set_update.setOnClickListener(this);

		save.setOnClickListener(this);
		cancle.setOnClickListener(this);
		// 设置行政区
		set_xzq.setOnClickListener(this);
		fanhui.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.set_update:
			// 检测更新
			setUpdataVersion();
			break;
		case R.id.save:
			// 保存设置
			saveSettings();
			break;
		case R.id.cancle:
			// 取消
			finish();
			break;
		case R.id.set_xzq:
			// 设置行政区
			showSynDialog(SystemSettingActivity.this);
			break;
		case R.id.set_dzjmjkk:
			// 设置居民电子档案签
			Intent intent = new Intent(SystemSettingActivity.this, SystemSettingJMDZDAActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_back:
			// 返回
			finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 设置更新
	 * 
	 * @author 王婷婷
	 * @date 2017年12月11日 下午4:34:34
	 */
	private void setUpdataVersion() {
		try {
			if (!GlobalConts.isNetworkAvailable(SystemSettingActivity.this)) {
				showTextDialog("请检查网络");
				return;
			}
			UpdateVersion up = new UpdateVersion(SystemSettingActivity.this);
			up.setSetDialogFlag(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存数据
	 * 
	 * @author 王婷婷
	 * @date 2017年12月11日 下午4:35:26
	 */
	private void saveSettings() {
		// Editor editor = MyPreferences.edit();
		// editor.putString("ftpAddress",
		// ftp_path.getText().toString());
		// editor.putString("ftpAccount",
		// ftp_account.getText().toString());
		// editor.putString("ftpPassword",
		// ftp_passward.getText().toString());
		// editor.putString("db_path", db_path.getText().toString());
		// editor.putString("date_tongbu_path",
		// date_tongbu_path.getText().toString());
		// editor.putString("shangchuan_yanzheng",
		// shangchuan_yanzheng.getText().toString());
		// editor.putString("shengji_hao",
		// shengji_hao.getText().toString());
		// editor.putString("zidong_shengji",
		// zidong_shengji.getText().toString());
		// editor.commit();
		SystemSettings sys = new SystemSettings();
		sys.setFtpAddress(ftp_path.getText().toString());
		sys.setFtpAccount(ftp_account.getText().toString());
		sys.setFtpPassword(ftp_passward.getText().toString());
		sys.setDb_path(db_path.getText().toString());
		sys.setDate_tongbu_path(date_tongbu_path.getText().toString());
		sys.setShangchuan_yanzheng(shangchuan_yanzheng.getText().toString());
		sys.setShengji_hao(shengji_hao.getText().toString());
		sys.setZidong_shengji(zidong_shengji.getText().toString());
		SPUtil.setSysSettings(SystemSettingActivity.this, sys);
		SPUtil.setSTR_UPLOAD_ADDRESS(SystemSettingActivity.this,
				TextUtils.isEmpty(et_uploadaddress.getText().toString().trim()) ? ""
						: et_uploadaddress.getText().toString().trim());
		SPUtil.setSTR_UPLOAD_JTYS_ADDRESS(SystemSettingActivity.this,
				TextUtils.isEmpty(et_upload_jtys_address.getText().toString().trim()) ? ""
						: et_upload_jtys_address.getText().toString().trim());
		NovaApplication.updateUrl = TextUtils.isEmpty(et_uploadaddress.getText().toString().trim()) ? ""
				: et_uploadaddress.getText().toString().trim();
		NovaApplication.updateJTYSUrl = TextUtils.isEmpty(et_upload_jtys_address.getText().toString().trim()) ? ""
				: et_upload_jtys_address.getText().toString().trim();

		Constants.AREA_CODE  = et_zzjgCode.getText().toString().trim();
		SPUtil.saveStr(mContext,SP_NAME,STR_ORIGIN_CODE,et_zzjgCode.getText().toString().trim());
		GlobalConts.showToast(SystemSettingActivity.this, "保存成功");


		finish();
	}
}
